package com.neu.servlet.wty.demo;

import com.neu.service.wty.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.Map;

@WebServlet(name = "CheckLoginServlet", urlPatterns = {"/CheckLoginServlet.do"})
public class CheckLoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("进入CheckLoginServlet");
        Cookie[] cookies=request.getCookies();
        if (cookies.length==1){
            response.sendRedirect("mgr/login.jsp");
        }else {
            Cookie c=null;
            for (Cookie cookie:cookies){
                if (cookie.getName().equals("info")){
                    c=cookie;
                    break;
                }
            }
            if (c==null){
                response.sendRedirect("mgr/login.jsp");
                System.out.println("未找到cookie'info'");
            }else {
                System.out.println("找到cookie,读取中...");
                String value=c.getValue();
                String []values=value.split(":");
//                request.getRequestDispatcher("MgrLoginServlet.do?mgr_no="+values[0]+"&mgr_pwd"+values[1])
//                       .forward(request,response);
                UserService mgrService=new UserService();
                Map<String,String> item=null;
                if (item==null){
                    response.sendRedirect("index.jsp");
                    System.out.println("cookies账户名密码验证失败!");
                }else {
                    System.out.println("cookies账户名密码验证成功!!!");
                    HttpSession session=request.getSession();
                    session.setAttribute("item",item);
                    request.getRequestDispatcher("WEB-INF/mgr/mgr_main.jsp").forward(request,response);
                }
            }
        }
    }


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}